#include "types.h"

void LevelObject::paint(QPainter *painter)
{
    for (auto ball_item = balls.begin();ball_item != balls.end();ball_item++) {
            ball_item->paint(painter);
    }


    baffle.paint(painter);

    score.paint(painter);

    for (auto block_item = blocks.begin();block_item != blocks.end();block_item++) {
            block_item->paint(painter);
    }
}
// 游戏执行，其中包含了游戏的碰撞算法等。
void LevelObject::run()
{
    for (auto ball_item = balls.begin();ball_item != balls.end();ball_item++) {
       // 保存当前小球的坐标，如果小球在运动过程中碰撞到物体，那么使其坐标回复到原始状态。防止小球进入到物体内部。
       double old_x = ball_item->getX();
       double old_y = ball_item->getY();
        ball_item->run();
        // 检测小球是否碰撞。
        // 1.小球是否碰撞边界
        // 2.小球是否碰撞挡板
        // 3.小球是否碰撞block
        int res = checkCollision(ball_item);
        // 碰撞成功，那么需要让小球会退到原始 位置。
        if(res != 0){
            ball_item->setPostion(static_cast<int>(old_x),static_cast<int>(old_y));
            ball_item->run();
            score.setInc();
        }
    }

}

int LevelObject::checkCollision(vector<CBall>::iterator ball_item)
{
    int res = 0;
    if(ball_item->getX() < 0 || ball_item->getX() > 800 - ball_item->getWidth()){

       ball_item->setHDirection(ball_item->getHDirection()*-1);
       ball_item->setHSpeed(3);
       ball_item->setVSpeed(1);
       res = 1;
    }

   if( ball_item->getY() > 800 - ball_item->getHeght() || ball_item->getY() < 0){

         ball_item->setVDirection(ball_item->getVDirection()*-1);
         ball_item->setHSpeed(2);
         ball_item->setVSpeed(3);
         res = 2;
    }

     // 挡板碰撞计算
    if((ball_item->getHeght() + baffle.getHeght())/2  >= abs(ball_item->getCY()- baffle.getCY())
            && ball_item->getCX() > baffle.getX()
            && ball_item->getCX() < baffle.getX() + baffle.getWidth()
            && (ball_item->getY() + ball_item->getHeght() > baffle.getY() ||
                ball_item->getY()< baffle.getY() - baffle.getHeght())){
            if(res != 3)
            ball_item->setVDirection(ball_item->getVDirection()*-1);
             res = 3;

    }
    if((ball_item->getWidth() + baffle.getWidth())/2  >= abs(ball_item->getCX()- baffle.getCX())
            && ball_item->getCY() > baffle.getY()
            && ball_item->getCY() < baffle.getY() + baffle.getHeght()
            && (ball_item->getX() + ball_item->getWidth() > baffle.getX() ||
                ball_item->getX()< baffle.getX() - baffle.getWidth()))

        {
            if(res != 1)
            ball_item->setHDirection(ball_item->getHDirection()*-1);
             res = 4;
        }


    // 检测小球和block的碰撞

    for (auto block_item = blocks.begin(); block_item != blocks.end();block_item++) {
        // 挡板碰撞计算
        if(block_item->getLive() <=0){
            continue;
        }
       if((ball_item->getHeght() + block_item->getHeght())/2  >= abs(ball_item->getCY()- block_item->getCY())
               && ball_item->getCX() > block_item->getX()
               && ball_item->getCX() < block_item->getX() + block_item->getWidth()
               && (ball_item->getY() + ball_item->getHeght() > block_item->getY() ||
                   ball_item->getY()< block_item->getY() - block_item->getHeght())){
               if(res != 3)
               ball_item->setVDirection(ball_item->getVDirection()*-1);
               block_item->setDec();
                res = 3;

       }
       if((ball_item->getWidth() + block_item->getWidth())/2  >= abs(ball_item->getCX()- block_item->getCX())
               && ball_item->getCY() > block_item->getY()
               && ball_item->getCY() < block_item->getY() + block_item->getHeght()
               && (ball_item->getX() + ball_item->getWidth() > block_item->getX() ||
                   ball_item->getX()< block_item->getX() - block_item->getWidth()))

           {
               if(res != 1)
               ball_item->setHDirection(ball_item->getHDirection()*-1);
               block_item->setDec();
                res = 4;
           }
    }

    return  res;
}
